package com.ks.app.admin.service.impl;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.persistence.Query;
import org.springframework.stereotype.Service;
import com.ks.app.admin.hibernate.KsOperaterLog;
import com.ks.app.admin.hibernate.KsUser;
import com.ks.app.admin.service.KsoperatelogService;
import com.ks.app.admin.service.UserService;
import com.ks.app.admin.util.Pager;

/***
 * 每个serviceimpl 必须实现BaseServiceImpl
 * @author Administrator
 *
 */
@Service("userService")
public class UserServiceImpl extends BaseServiceImpl implements UserService {
	@Resource
	private KsoperatelogService ksoperatelogService;

	@SuppressWarnings("unchecked")
	public List<KsUser> GetKsUser(String uiid) {
		String sql = "from KsUser where ksUserId='" + uiid + "'";
		System.out.println(sql);
		return em.createQuery(sql).getResultList();
	}

	@SuppressWarnings("unchecked")
	public List<KsUser> GetKsUserList(String uiid, Pager pager) {
		String sql = " from KsUser where 1=1 ";
		if (uiid != null && uiid.length() > 0) {
			sql = sql + "  and ksUserId=? ";
		}
		Query query = em.createQuery(sql + " order by ksUserId desc ");
		if (uiid != null && uiid.length() > 0) {
			long mid = Long.parseLong(uiid);
			query.setParameter(1, mid);
		}
		query.setMaxResults(pager.getCntOfPage());
		query.setFirstResult((pager.getCurPage() - 1) * pager.getCntOfPage());
		pager.setTotoalCnt(GetcntKsUser(uiid));
		List<KsUser> list = query.getResultList();
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<KsUser> GetKsUser(String username, String pwd) {
		String sql = "from KsUser where username='" + username + "'";
		return em.createQuery(sql).getResultList();
	}

	public List<KsUser> GetKsUserByUserGroup(String Group) {
		return null;
	}

	public List<KsUser> GetKsUserByUsername(String Username) {
		return null;
	}


	public long addKsUser(KsUser KsUser, String username) {
		try {
			// 添加日志
			String uno = KsUser.getKsUserNo();
			String uname = KsUser.getUsername();
			// long uid=KsUser.getKsUserId();
			String gp = KsUser.getKsUserGroup();
			String cname = KsUser.getKsCuserName();
			em.persist(KsUser);
			long res = KsUser.getKsUserId();
			KsOperaterLog ksOperateLog = new KsOperaterLog();
			ksOperateLog.setContent(username + "添加了用户:[id=" + res + "];[编号="
					+ uno + "]；[中文名称=" + cname + "];[登录名称=" + uname + "]；用户组="
					+ gp);
			ksOperateLog.setLogtime(new Date());
			ksOperateLog.setOperater(username);
			ksoperatelogService.save(ksOperateLog);
			return res;
		} catch (Exception ee) {
			ee.printStackTrace();
			return 0;
		}
	}

	public int deleteKsUser(KsUser ksuser, String username) {
		em.remove(em.merge(ksuser));
		return 0;
	}

	@SuppressWarnings("unchecked")
	public List<KsUser> getKsUser() {
		String sql = "from KsUser";
		return em.createQuery(sql).getResultList();
	}

	public String login(String ksuser) {
		return null;
	}

	public int saveKsUser(KsUser ksuser) {
		return 0;
	}

	public long updateKsUser(KsUser KsUser, String username) {
		long res = -1;
		try {

			String uno = KsUser.getKsUserNo();
			String uname = KsUser.getUsername();
			long uid = KsUser.getKsUserId();
			String gp = KsUser.getKsUserGroup();
			String cname = KsUser.getKsCuserName();
			em.merge(KsUser);
			res = 1;

			KsOperaterLog ksOperateLog = new KsOperaterLog();

			ksOperateLog.setContent(username + "修改了用户:[id=" + uid + "];[编号="
					+ uno + "]；[中文名称=" + cname + "];[登录名称=" + uname + "]；用户组="
					+ gp);
			ksOperateLog.setLogtime(new Date());
			ksOperateLog.setOperater(username);
			ksoperatelogService.save(ksOperateLog);
		} catch (Exception e) {

		}

		return res;
	}

	public int GetcntKsUser(String rid) {
		String sql = "from KsUser where 1=1 ";
		if (rid != null && rid.length() > 0) {
			sql = sql + " and ksUserId=? ";
		}
		Query query = em.createQuery(sql);
		if (rid != null && rid.length() > 0) {
			long mid = Long.parseLong(rid);
			query.setParameter(1, mid);
		}
		return query.getResultList().size();
	}
}
